#include "grid.h"

/**
 * @brief 读入坐标和面积
 * 
 * @param gridFileName 网格文件名
 */
void Grid::rgrid(const string &gridFileName) noexcept
{
    ifstream gridFile{gridFileName, std::ios_base::in};
    if (!gridFile)
    {
        // throw std::runtime_error{"cannot open Grid file\n"};
        std::cerr << "cannot open Grid file\n";
        std::exit(EXIT_FAILURE);
    }

    gridFile >> imax;
    x.resize(imax);
    area.resize(imax);
    vol.resize(imax);
    ib2 = imax - 1;
    ncells = imax - 3;

    for(size_t i = 0; i < imax; ++i)
    {
        gridFile >> x[i] >> area[i];
    }
    gridFile.close();
}
/**
 * @brief 计算体积
 * 
 */
void Grid::inigrid() noexcept
{
    for(size_t i = 1; i < ib2; ++i)
    {
        vol(i) = (area(i-1) + 2.0*area(i) + area(i+1))*(x(i+1) - x(i-1))/8.0;
    }
    vol(0) = vol(1);
    vol(imax-1) = vol(ib2-1);
}
